//
//  HCHelper.m
//  ChatForHot
//
//  Created by wr on 2018/10/11.
//  Copyright © 2018年 FLY. All rights reserved.
//

#define HOTCHATORIGINAL_MAX_WIDTH 640.0f

#import "HCHelper.h"

@implementation HCHelper

+ (instancetype)sharedInstance
{
    static HCHelper *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

static CGFloat pixelOne = -1.0f;
+ (CGFloat)pixelOne {
    if (pixelOne < 0) {
        pixelOne = 1 / [[UIScreen mainScreen] scale];
    }
    return pixelOne;
}

- (UIImage *)HOTCHATimageCompressToMaxSize:(UIImage *)sourcePic {
    if (sourcePic.size.width < HOTCHATORIGINAL_MAX_WIDTH) return sourcePic;
    CGFloat btWidth = 0.0f;
    CGFloat btHeight = 0.0f;
    if (sourcePic.size.width > sourcePic.size.height) {
        btHeight = HOTCHATORIGINAL_MAX_WIDTH;
        btWidth = sourcePic.size.width * (HOTCHATORIGINAL_MAX_WIDTH / sourcePic.size.height);
    } else {
        btWidth = HOTCHATORIGINAL_MAX_WIDTH;
        btHeight = sourcePic.size.height * (HOTCHATORIGINAL_MAX_WIDTH / sourcePic.size.width);
    }
    CGSize targetSize = CGSizeMake(btWidth, btHeight);
    return [self HOTCHATimageCroppingForSourcePic:sourcePic sourceSize:targetSize];
}

- (UIImage *)HOTCHATimageCroppingForSourcePic:(UIImage *)sourceImage sourceSize:(CGSize)targetSize {
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else
            if (widthFactor < heightFactor)
            {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
    }
    UIGraphicsBeginImageContext(targetSize); // this will crop
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (void)HOTCHATDeleteFileWithPath:(NSString *)pathString {
    if([[NSFileManager defaultManager] fileExistsAtPath:pathString]) {
        NSError *error=nil;
        [[NSFileManager defaultManager] removeItemAtPath:pathString error:&error];
    }
}
@end
